import * as THREE from "three";
import { loadFbx } from "./loadModel"
import { getBox } from "./getBox"
import cabinetModel from '@/cabinetModel/index'
export function createDeviceSlot({ height, stepHeight }) {
    let slot = new THREE.Object3D();
    slot.type = 'DeviceSlot'
    slot.position.y += (height - 1) * stepHeight;
    return slot
}

/**
 * 从机柜数据加载所有设备
 * @param data 机柜数据
 */
export async function addCabinetDevices(data, { parentObj, deviceSlotHeight }) {
    for (let deviceData of data.views) {

        if(deviceData.brand == 'PDU') continue
        let slot = createDeviceSlot({
            height: deviceData.pos,
            stepHeight: deviceSlotHeight,
        });

        console.log("devices :", deviceData)

        /*
        let obj = await loadFbx(`model/device/${deviceData.model_name}.FBX`);
        obj.type = "Device";
        obj.userData = {
            id: deviceData.id,
        };
        let box = getBox(obj);
        obj.__3dd = {
            localBox: box,
        }
        */

         // 创建一个立方体
        
         new cabinetModel({
            data:deviceData,
            parent:slot,
            deviceSlotHeight:deviceSlotHeight
         })
        
         /*
        let dh = deviceSlotHeight * deviceData.height

        const boxGeometry = new THREE.BoxGeometry(0.5, dh, 0.5);
        const boxMaterial = new THREE.MeshBasicMaterial({ color: '#008000' });
        const box = new THREE.Mesh(boxGeometry, boxMaterial);
        //box.position.x = 0; // 将立方体向右移动一点
        slot.add(box);
         */

        //slot.add(obj);
        parentObj.add(slot);
    }
}

export function getDevices(root) {
    let objs = []
    root.traverse(obj => {
        if (obj.type === "Device") {
            objs.push(obj);
        }
    });
    return objs
}
